package org.example.ag_notes.service;
import org.example.ag_notes.entity.User;
import org.example.ag_notes.exception.AuthenticationException;
import org.example.ag_notes.repository.UserRepository;
import org.example.ag_notes.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public abstract class BaseService {

    @Autowired
    protected AuthService authService;

    @Autowired
    protected UserRepository userRepo;

    protected User validateAndGetUser(String accessToken) {
        Integer userId = authService.getIDbyToken(accessToken);
        if (userId == null) {
            throw new AuthenticationException("无效的Token");
        }

        return userRepo.findById(userId)
                .orElseThrow(() -> new AuthenticationException("无效用户"));
    }


}
